<?php

$di     = \application\units\Di::getInstance();
$params = [
    'id'       => $di->request->params('id'),
    'admin_id' => $di->auth->id,
];

$authRoleIds = [];
if ($params['admin_id']) {
    $adminIds = $di->auth->getChildrenRoleAdminIds($params['admin_id'], true);
    $authRoleIds = $di->auth->getRoleIdsByAdminId($params['admin_id']);
} else {
    $adminIds = [$params['admin_id']];
}

$row = $di->db->find('select * from admin where id=:id and id in (:ids)', [
    'id'  => $params['id'],
    'ids' => $adminIds,
]);

if (!$row) throw new LogicException('未找到记录');

$roles = $di->db->query('select a.admin_id,g.* from admin_role_access a
            join admin_role g on g.id=a.role_id
            where admin_id in (:admin_id)', [
    'admin_id' => $params['id'],
]);

if ($roles) {
    $row['role_id']   = implode(',', array_column($roles, 'id'));
    $row['role_text'] = implode(',', array_column($roles, 'name'));
}

// 不能操作自己拥有的权限
if (array_intersect(explode(',', $row['role_id']), $authRoleIds)) {
    $row['disabled'] = 1;
} else {
    $row['disabled'] = 0;
}

$list = [
    'status' => [1 => '正常', 0 => '禁用'],
];

$row['status_text'] = $list['status'][$row['status']];

return [
    'row' => $row,
];